
$(function () {
    //js和jsx的交互接口
    var isLock=true;
    var cs = new CSInterface();

    $(".color-item-content").mouseenter(function(){
        //不是输入状态
        if($("#txtName").css("display")=="none")
        {
            if($(this).attr('have-color')=="1")
            {
                $("#currentColor").text($(this).attr("color-name")+"--["+$(this).attr("R")+","+$(this).attr("G")+","+$(this).attr("B")+"]");
                $(this).css("border","2px solid #F2F2F2");
                $(this).css("cursor","pointer");
            }else
            {
                $("#currentColor").text("未设置");
            }
        }
    });

    $(".color-item-content").mouseleave(function(){
        if($("#txtName").css("display")=="none")
        {
            $(this).css("border","2px solid #535353");
            $(this).css("cursor","default");
        }
    });

    $("#btnLock").click(function(){
        if(isLock){
            $("#btnLock").attr('src',"img/unlock.png"); 
        }else
        {
            $("#btnLock").attr('src',"img/lock.png"); 
        }
        isLock=!isLock;
    });

    $(".color-item-content").bind("contextmenu", function () {
        if(isLock)
        {
            $("#currentColor").css("display","none");
            $("#btnSaveName").css("display","inline-block");
            $("#txtName").css("display","inline-block");
            $("#btnSaveName").attr("current-id",$(this).attr("id"));
            $(this).css("border","2px solid #F2F2F2");
            $(this).css("cursor","pointer");
            if($(this).attr("color-name")=="未设置"){
                $("#txtName").val("");
            }else
            {
                $("#txtName").val($(this).attr("color-name"));
            }
        }
        return false;
    });

    $("#btnSaveName").click(function(){
        if(isLock)
        {
            var txtcontent=$("#txtName").val();
            if(txtcontent!="")
            {
                $("#"+$("#btnSaveName").attr("current-id")).attr("color-name",txtcontent);
            }
            $("#btnSaveName").css("display","none");
            $("#txtName").css("display","none");
            $("#currentColor").css("display","inline-block");
            $("#currentColor").text(txtcontent+"--["+$("#"+$("#btnSaveName").attr("current-id")).attr("R")+","+$("#"+$("#btnSaveName").attr("current-id")).attr("G")+","+$("#"+$("#btnSaveName").attr("current-id")).attr("B")+"]");
        }
        
    });

    $(".color-item-content").bind("click",function(){
        if(isLock)
        {
            if($(this).attr("have-color")=="1")
            {
                if($("#txtName").css("display")=="none")
                {
                    cs.evalScript("updateForeBackground("+$(this).attr("R")+","+$(this).attr("G")+","+$(this).attr("B")+")");
                }
            }
        }else
        {
            var that=this;
            cs.evalScript("saveSelectColor()",function(res){
                var color=res.split('|');
                var R=Math.round(parseInt(color[0]));
                var G=Math.round(parseInt(color[1]));
                var B=Math.round(parseInt(color[2]));
                $(that).attr("R",R);
                $(that).attr("G",G);
                $(that).attr("B",B);
                $(that).css("background","rgba("+R+","+G+","+B+",1)");
            });
            $(this).attr('have-color',"1"); 
        }
    });

});





